/*
 * This file is part of quickLoad, a software for gamers
 *
 * Copyright (C) 2005-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Specialized;

namespace CookieTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string CookieRegex = @"COOKIE\s(.*?)\sesl.eu.*\s.*\s(\d+)\s(\d+)\s";
        string CookieFileRegex = @".*?@esl(\[\d+\])*.txt";
        string FFCookieRegex = @".esl.eu\s.*\s/\s.*\s(\d+)\sCOOKIE\s(.*)\s";
        void getCookies()
        {
            StreamReader s=null;
            Match m;
            String content,cookie;

            DirectoryInfo iedir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
            DirectoryInfo ffdir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + "Mozilla" + Path.DirectorySeparatorChar + "Firefox" + Path.DirectorySeparatorChar + "Profiles");
            StringCollection c = new StringCollection();
            foreach (FileInfo f in iedir.GetFiles("*.txt"))
            {
                if (Regex.Match(f.Name, CookieFileRegex, RegexOptions.Singleline).Success)
                //c.Add(f.FullName);
                {
                    try
                    {
                        s = new StreamReader(f.FullName);
                        content = s.ReadToEnd();
                        s.Close();
                        if ((m = Regex.Match(content, CookieRegex, RegexOptions.Singleline)).Success)
                        {
                            UInt64 time = Convert.ToUInt64(m.Groups[2].Value) + (Convert.ToUInt64(m.Groups[3].Value) << 32);
                            DateTime dt = DateTime.FromFileTimeUtc((long)time);
                            tb.Text += "IE Cookie found: " + m.Groups[1].Value + ". Expires: " + dt.ToString() + Environment.NewLine;
                        }
                    }
                    catch
                    {
                        if (s != null)
                            s.Close();
                    }
                }

            }
            foreach (DirectoryInfo d in ffdir.GetDirectories())
            {
                try
                {
                    s = new StreamReader(d.FullName + Path.DirectorySeparatorChar + "cookies.txt");
                    content = s.ReadToEnd();
                    s.Close();
                    m = Regex.Match(content, FFCookieRegex);
                    if(m.Success)
                    {
                        DateTime dt2 = new DateTime(1970, 1, 1).AddSeconds(Convert.ToUInt64(m.Groups[1].Value));
                        tb.Text += "FF Cookie found: " + m.Groups[2].Value + ". Expires: " + dt2.ToString() + Environment.NewLine;
                    }
                }
                catch
                {
                    if (s != null)
                        s.Close();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            getCookies();
        }
    }
}